const fn1 = (i) => {
    return i + 1
}

const fn2 = (i) => {
    return i + 2
}
const fn3 = (i) => {
    return i + 3
}
const fn4 = (i) => {
    return i + 4
}

const a = compose(fn1, fn2, fn3, fn4)
// console.log(a(1)); // 1+2+3+4 = 11
/**
 * 手写一个函数,需要满足下面的要求
 * 这里返回值是一个函数
 * 需要将上一个函数的结果作为当前函数的参数
 * 需要知道当前参数是传入了多少个函数,不然就是无数遍的重复
 */

function compose() {
    const funList = [...arguments]
    return function (num) {
        return funList.reduce((pre, cur) => cur(pre), num)
    }
}


function compose1() {
    const funList = [...arguments]
    // console.log(funList);
    return function (num) {
        return funList.reduce((pre, cur) => cur(pre), num)
    }
}
// const res = compose1(fn1, fn2, fn3, fn4)
// console.log(res(1));


function compose2(num) {
    // slice 获取从1到最后的元素，就是直接写一个begin数字
    const funList = [...arguments].slice(1);
    console.log(funList);
}

// const res1 = compose2(1, fn1, fn2, fn3, fn4)

function compose3() {
    const funcList = [...arguments], length = funcList.length

    return function (...arg) {
        var index = 0
        var preResult = length ? funcList[index](...arg) : arg

        while (++index < length) {
            preResult = funcList[index](preResult)
        }
        return preResult;
    }
}

const markFunc = compose3(fn1, fn2, fn3, fn4)
const markFunc1 = compose3()
console.log(markFunc(1))
console.log(markFunc1(1222))


/**
 * 组合函数，传进去一个个的函数，然后依次执行这个函数
 * @returns function
 */
function compose4() {
    const funcList = [...arguments]
    // 输出一个函数
    return function (...args) {
        return funcList.reduce((pre, cur) => cur(pre), ...args)
    }
}